﻿/**
 * Created by Maibenben on 2017/5/3.
 */
$(function () {
    //跳转到本页之后读取cookie中是否有mid和token
    checkCookie();
    var mid = $.cookie("mid");
    var token = $.cookie("token");
    var important = "&mid=" + mid + "&token=" + token;
    var databack = true;
    var postchecked = false;
    var calendar = new lCalendar();
    calendar.init({
        'trigger': '.user-birthday',
        'type': 'date'
    });
    //倒计时
    var countdown = localStorage.getItem("countdownref");
    var remainsecond = countdown;
    if (countdown) {
        $(".codebtn").attr("disabled", true);
        var timer = setInterval(function () {
            remainsecond--;
            localStorage.setItem("countdownref", remainsecond);
            if (remainsecond <= 0) {
                localStorage.removeItem("countdownref");
                clearInterval(timer);
                $(".codebtn").val("重新获取").attr("disabled", false);
            } else {
                $(".codebtn").val(remainsecond + "s后可重新获取");
            }
        }, 1000);
    }
    //选择性别
    $(".user-gender").on("click", function () {
        $(".choose_gender").show();
    });
    $(".choose_gender>div").on("click", function () {
        $(".choose_gender").hide();
        $(".user-gender").text($(this).text()).attr("data-gender", $(this).attr("data-gid"));
    });
    //选择关系
    $(".user-relation").on("click", function () {
        $(".choose_relation").toggle(100);
    });
    $(".choose_relation>div").on("click", function () {
        $(".choose_relation").hide(100);
        $(".user-relation").text($(this).text()).attr("data-relation", $(this).attr("data-rid"));
    });
    //点击获取验证码
    $(".codebtn").on("click", function () {
        var telreg = /^1(3|4|5|7|8)\d{9}$/;
        if (!telreg.test($(".user-tel").val())) {
            creatLayout("请输入正确的手机号码");
            return false;
        }
        //倒计时
        localStorage.setItem("countdownref", 60);
        var remainsecond = localStorage.getItem("countdownref");
        $(this).attr("disabled", true);
        var timer = setInterval(function () {
            remainsecond--;
            localStorage.setItem("countdownref", remainsecond);
            if (remainsecond <= 0) {
                localStorage.removeItem("countdownref");
                clearInterval(timer);
                $(".codebtn").val("重新获取").attr("disabled", false);
            } else {
                $(".codebtn").val(remainsecond + "s后可重新获取");
            }
        }, 1000);
        //发送短信验证
        $.get(checkcodeURL + "?phone=" + $(".user-tel").val() + "&type=2" + important, function (res) {
                postchecked = true;
                creatLayout(res.msg);
            }
        );
    });
    //给保存按钮绑定事件，实现发送数据到后台进行保存
    $(".save").on("click", function () {
        var that = $(this);
        if (databack) {
            //将用户输入的数据发送到服务器进行储存
            var data = {
                mid: $.cookie("mid"),
                token: $.cookie("token"),
                mobile: $(".user-tel").val(),
                relation: $(".user-relation").attr("data-relation"),
                //id_card: $(".user-idcard").val(),
                name: $(".user-name").val(),
                sex: $(".user-gender").attr("data-gender"),
                birthday: $(".user-birthday").val(),
                type: 2,
                captcha: $(".checkcode").val(),
                phone: $(".user-tel").val()
            };
            if (data.name == "") {
                creatLayout("姓名不能为空");
                return false;
            }
            if (data.birthday == "") {
                creatLayout("出生日期不能为空");
                return false;
            }
            var timestamp = Date.parse(new Date(data.birthday));
            if (timestamp > new Date().getTime()) {
                creatLayout("出生日期属于不合法输入");
                return false;
            }
            /*
             var idreg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
             if (!idreg.test(data.id_card)) {
             creatLayout("请输入正确的身份证号码");
             return false;
             }
             */
            var telreg = /^1(3|4|5|7|8)\d{9}$/;
            if (!telreg.test(data.mobile)) {
                creatLayout("请输入正确的手机号码");
                return false;
            }
            if ($(".checkcode").val() == "") {
                creatLayout("请填写验证码");
                return false;
            }
            that.css("background", "#ccc");
            databack = false;
            $.post(addSaveURL, data, function (res) {
                databack = true;
                that.css("background", "rgb(47, 178, 159)");
                //储存之后返回之前的页面，并且显示最新的数据
                if (res.state == 1) {
                    creatLayout("添加成功");
                    setTimeout(function () {
                        window.location.href = "finish-reference.html";
                    }, 200);
                } else {
                    creatLayout("添加失败");
                }
            });
        }
    });
});